package cn.luowenjie.faceclock.clock;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.widget.Toolbar;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import cn.luowenjie.faceclock.R;
import cn.luowenjie.faceclock.database.DatabaseHelper;

public class SetClockActivity extends AppCompatActivity implements View.OnClickListener {

    private TimePicker timePicker;
    private Toolbar toolbar;
    private TextView toolbar_title, tv_cancel, tv_save;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_clock);
        toolbar = findViewById(R.id.clockset_toolbar);
        toolbar_title = findViewById(R.id.tb_title);
        intent = getIntent();
        initToolBar();
        initClock();

        tv_cancel = findViewById(R.id.tv_cancel);
        tv_save = findViewById(R.id.tv_save);
        tv_cancel.setOnClickListener(this);
        tv_save.setOnClickListener(this);

    }

    private void initToolBar(){
        String mode = intent.getStringExtra("mode");
        if(mode.equals("add")) {
            toolbar_title.setText("添加闹钟");
        }
        else if(mode.equals("edit")) {
            toolbar_title.setText("设置闹钟");
        }
        else {
            toolbar_title.setText("闹钟设置"+mode);
        }
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    private void initClock() {
        timePicker = findViewById(R.id.mtimepicker);
        timePicker.setIs24HourView(true);
        int hourId = Resources.getSystem().getIdentifier("hour", "id", "android");//小时数字选择器
        int dividerId = Resources.getSystem().getIdentifier("divider", "id", "android");//时分分隔的那个冒号
        int minuteId = Resources.getSystem().getIdentifier("minute", "id", "android");//分钟数字选择器
        //int amPmId = Resources.getSystem().getIdentifier("amPm", "id", "android");//上下午，有需要的朋友自己去尝试
        //NumberPicker ampm = timePicker.findViewById(amPmId);
        NumberPicker hourPicker = timePicker.findViewById(hourId);
        TextView textView = timePicker.findViewById(dividerId);
        textView.setText(":");//这个是时与分之间的分隔“：”
        textView.setTextColor(Color.WHITE);
        NumberPicker hoursPicker = timePicker.findViewById(hourId);
        NumberPicker minutePicker = timePicker.findViewById(minuteId);
       // NumberPicker ampmPicker = timePicker.findViewById(amPmId);
        //traverseViewGroup(start_time);
        setNumberPickerDivider(hoursPicker);
        setNumberPickerDivider(minutePicker);
        //setNumberPickerDivider(ampmPicker);
    }

    private void setNumberPickerDivider(NumberPicker numberPicker) {
        final int count = numberPicker.getChildCount();
        for(int i = 0; i < count; i++){
            try{
                Field dividerField = numberPicker.getClass().getDeclaredField("mSelectionDivider");
                dividerField.setAccessible(true);
                ColorDrawable colorDrawable = new ColorDrawable(
                        ContextCompat.getColor(this, R.color.white));
                dividerField.set(numberPicker,colorDrawable);
                numberPicker.invalidate();
            }
            catch(NoSuchFieldException | IllegalAccessException | IllegalArgumentException e){
                Log.w("setNumberPickerTxtClr", e);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_save:
                //this.deleteDatabase("faceclock");
                DatabaseHelper databaseHelper = new DatabaseHelper(this,"faceclock",null,1);
                SQLiteDatabase db = databaseHelper.getWritableDatabase();

                ContentValues values = new ContentValues();
                values.put("hour", timePicker.getHour());
                values.put("minute", timePicker.getMinute());
                db.insert("clocks",null,values);

                finish();
        }
    }

}